package org.example.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.annotation.RequestInfo;
import org.example.entity.control.BmsControlDTO;
import org.example.service.impl.BmsControlService;
import org.example.util.Result;
import org.example.util.constantUtil.ConstantUtil;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/control")
@CrossOrigin
@Slf4j
@RequiredArgsConstructor
public class BmsControlController {
    private final BmsControlService bmsControlService;

    @RequestInfo
    @PostMapping("/set")
    public Result bmsControl(@RequestBody BmsControlDTO bmsControlDTO) {
        if (bmsControlDTO.getImei() == null) {
            return Result.fail("缺少设备imei号");
        }
        if (bmsControlDTO.getFunctionCode() == null) {
            return Result.fail("缺少控制功能码");
        }
        if (bmsControlDTO.getControlCode() == null) {
            return Result.fail("缺少控制数据");
        }
        if (bmsControlDTO.getControlCode().length > ConstantUtil.FRAME_C2_BYTES - 1) {
            return Result.fail("传入了过多的控制数据");
        }
        return bmsControlService.bmsControl(bmsControlDTO);
    }
}
